package com.atguigu.gmall.oms.service.impl;

import com.alibaba.fastjson.JSON;
import com.atguigu.gmall.common.bean.ResponseVo;
import com.atguigu.gmall.oms.entity.OrderItemEntity;
import com.atguigu.gmall.oms.feign.GmallPmsClient;
import com.atguigu.gmall.oms.mapper.OrderItemMapper;
import com.atguigu.gmall.oms.vo.OrderItemVo;
import com.atguigu.gmall.oms.vo.OrderSubmitVO;
import com.atguigu.gmall.pms.entity.*;
import com.atguigu.gmall.ums.entity.UserAddressEntity;
import com.atguigu.gmall.ums.entity.UserEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.gmall.common.bean.PageResultVo;
import com.atguigu.gmall.common.bean.PageParamVo;

import com.atguigu.gmall.oms.mapper.OrderMapper;
import com.atguigu.gmall.oms.entity.OrderEntity;
import com.atguigu.gmall.oms.service.OrderService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;


@Service("orderService")
public class OrderServiceImpl extends ServiceImpl<OrderMapper, OrderEntity> implements OrderService {
    @Autowired
    OrderMapper orderMapper;

    @Autowired
    OrderItemMapper orderItemMapper;

    @Autowired
    GmallPmsClient pmsClient;

    @Override
    public PageResultVo queryPage(PageParamVo paramVo) {
        IPage<OrderEntity> page = this.page(
                paramVo.getPage(),
                new QueryWrapper<OrderEntity>()
        );

        return new PageResultVo(page);
    }

    //创建订单
    @Transactional
    @Override
    public void saveOrder(OrderSubmitVO orderSubmitVO, String userId) {
        //本质是向数据库的两张表中填写数据
        //1、order表
        OrderEntity orderEntity = new OrderEntity();
        //设置用户信息
        orderEntity.setUserId(Long.valueOf(userId));
//        orderEntity.setUseIntegration(user.getIntegration().intValue());
//        orderEntity.setUsername(user.getUsername());
        //设置订单号
        orderEntity.setOrderSn(orderSubmitVO.getOrderToken());
        //设置创建时间
        orderEntity.setCreateTime(new Date());

        //设置金额相关
        orderEntity.setTotalAmount(orderSubmitVO.getTotalPrice());
        //设置支付方式
        orderEntity.setPayType(orderSubmitVO.getPayType());
        orderEntity.setSourceType(0);
        orderEntity.setStatus(0);
        orderEntity.setDeliveryCompany(orderSubmitVO.getDeliveryCompany());
        UserAddressEntity address = orderSubmitVO.getAddress();
        if (address != null) {
            orderEntity.setReceiverAddress(address.getAddress());
            orderEntity.setReceiverCity(address.getCity());
            orderEntity.setReceiverName(address.getName());
            orderEntity.setReceiverPhone(address.getPhone());
            orderEntity.setReceiverPostCode(address.getPostCode());
            orderEntity.setReceiverProvince(address.getProvince());
            orderEntity.setReceiverRegion(address.getRegion());
        }

        orderEntity.setConfirmStatus(0);
        orderEntity.setDeleteStatus(0);
        orderEntity.setUseIntegration(orderSubmitVO.getBounds());
        this.save(orderEntity);
        Long id = orderEntity.getId();

        //2、orderItem表
        List<OrderItemVo> items = orderSubmitVO.getItems();
        OrderItemEntity orderItem = new OrderItemEntity();
        items.forEach(item->{
           orderItem.setOrderId(id);
           orderItem.setOrderSn(orderSubmitVO.getOrderToken());
           orderItem.setSkuQuantity(item.getCount());
           //查询sku的相关信息
            ResponseVo<SkuEntity> skuEntityResponseVo = this.pmsClient.querySkuById(item.getSkuId());
            SkuEntity skuEntity = skuEntityResponseVo.getData();
            if (skuEntity!=null){
            orderItem.setSpuId(skuEntity.getSpuId());
            orderItem.setSkuName(skuEntity.getName());
            orderItem.setSkuPic(skuEntity.getDefaultImage());
            orderItem.setSkuPrice(skuEntity.getPrice());
            orderItem.setRealAmount(skuEntity.getPrice());
            orderItem.setCategoryId(skuEntity.getCategoryId());
            //查询销售属性
            ResponseVo<List<SkuAttrValueEntity>> listResponseVo = this.pmsClient.querySaleAttrValuesBySkuId(item.getSkuId());
            List<SkuAttrValueEntity> skuAttrValueEntities = listResponseVo.getData();
            if (!CollectionUtils.isEmpty(skuAttrValueEntities)){
            orderItem.setSkuAttrsVals(JSON.toJSONString(skuAttrValueEntities));
            }
            //查询spu相关的信息
                ResponseVo<SpuEntity> spuEntityResponseVo = this.pmsClient.querySpuById(skuEntity.getSpuId());
                SpuEntity spuEntity = spuEntityResponseVo.getData();
                if (spuEntity!=null){
                    orderItem.setSpuName(spuEntity.getName());

                }
                ResponseVo<SpuDescEntity> spuDescEntityResponseVo = this.pmsClient.querySpuDescById(skuEntity.getSpuId());
                SpuDescEntity spuDescEntity = spuDescEntityResponseVo.getData();
                if (spuDescEntity!=null){
                    orderItem.setSpuPic(spuDescEntity.getDecript());
                }
            //品牌名称
                ResponseVo<BrandEntity> brandEntityResponseVo = this.pmsClient.queryBrandById(skuEntity.getBrandId());
                BrandEntity brandEntity = brandEntityResponseVo.getData();
                if (brandEntity!=null){
                    orderItem.setSpuBrand(brandEntity.getName());
                }
            }
        });
        this.orderItemMapper.insert(orderItem);
//        int i = 1/0;
    }

}